package appVersionRecord

import (
	"context"
	"github.com/google/uuid"
	"insighttracking/common/utils"
	"insighttracking/query/service"
	"net/http"

	"insighttracking/common/types"

	"github.com/zeromicro/go-zero/core/logx"
	"os"
)

type GetAppVersionRecordPageApiLogic struct {
	logx.Logger
	ctx     context.Context
	Request *http.Request
}

func NewGetAppVersionRecordPageApiLogic(ctx context.Context, request *http.Request) *GetAppVersionRecordPageApiLogic {
	hostname, _ := os.Hostname()
	return &GetAppVersionRecordPageApiLogic{
		Logger: logx.WithContext(ctx).WithCallerSkip(0).WithFields(
			logx.Field("Log_UUID", uuid.New().String()),
			logx.Field("hostname", hostname)),
		ctx:     ctx,
		Request: request,
	}
}

func (l *GetAppVersionRecordPageApiLogic) GetAppVersionRecordPageApi(req *types.AppVersionRecordReq) (resp *types.AppVersionRecordResp, err error) {
	// todo: add your logic here and delete this line
	appVersionRecordEntities, count, err := service.AppVersionRecordService.GetPageApi(l.ctx, req.Types, req.Version, req.PageNumber, req.PageSize)
	if err != nil || len(appVersionRecordEntities) == 0 {
		return &types.AppVersionRecordResp{
			Data: make([]types.AppVersionRecordModel, 0),
			PageBaseResp: types.PageBaseResp{
				PageSize:   req.PageSize,
				PageNumber: req.PageNumber,
				Total:      0,
			},
		}, nil
	}
	var result = make([]types.AppVersionRecordModel, 0)
	for _, item := range appVersionRecordEntities {
		result = append(result, types.AppVersionRecordModel{
			ID:        item.ID,                                 // 主键id
			Version:   item.Version,                            // 版本号
			FileURL:   item.FileURL,                            // 文件地址
			FileName:  item.FileName,                           // 文件名称
			Md5:       item.Md5,                                // MD5的值
			Types:     item.Types,                              // app版本类型,0表示分拣，1表示门店,2表示采购
			Remark:    item.Remark,                             // 备注
			CreatedAt: utils.TimestampToString(item.CreatedAt), // 创建时间
			UpdatedAt: utils.TimestampToString(item.UpdatedAt), // 更新时间
		})
	}
	return &types.AppVersionRecordResp{
		Data: result,
		PageBaseResp: types.PageBaseResp{
			PageSize:   req.PageSize,
			PageNumber: req.PageNumber,
			Total:      count,
		},
	}, nil
}
